import Vue from "vue";
import Vuex from "vuex";
import api from "../assets/js/request/api/index";
import router from "../router";
import ElementUI from "element-ui";

import conversation from "./modules/conversation";
import group from "./modules/group";
import imUser from "./modules/imUser";
import friend from "./modules/friend";
import blacklist from "./modules/blacklist";
import live from "./modules/live";

import modulePage from "./modules/modulePage";
import Cookies from "js-cookie";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    rentCate:[],
    rightsCate:[],
    rentList: [],
    rightsList: [],
    scenicList: [],
    materialCate: [],
    extractList: [],
    manageInfo: "",
    breadcrumbs: [],
    carInfoCate: [],
    taskCate: [],
    cityinfoCate: [],
    nowDsd: {}, //当前代收点
    leftopen: true,
    current: Date.now(), // 当前时间
    intervalID: 0,
    menuList: "",
    newsFeiLei: [],
    id: "",
    title: "",
    title2: "",
    showTitle2: false,
    tzUrl: "",
    parentTitle: "",
    sms_id: null,
    wx_material: "",
    updateWxText: "",
    updateNewRow: "",
    shopItemId: "",
    shopSlist: [],
    shopCateList: [],
    tourCateList: [],
    newsList: "",
    rewardOption: [],
    headTitleInfo: "",
    adminname: "",
    isservice: "",
    isNotice: "",
    coinReatting: [],
    sku: [],
    isLoading: false,
    plugin: {},
    address: [],
    ratting: [],
    adminTitle: "",
    phoneAddress: "",
    isMailOpen: false,
    chat_userinfo: {},
    formList: [],
    types_option: [],
    payList: [],
    courseCate: [],
    userComplaintType: [],
    bzList: [],
    rewardPlugin: [],
    moviesCate: [],
    footPrintType: [],
    coinCurrency: [],
    novelClassify: [],
    audioClassify: [],
    picTypeList: [],
    mobileWebSite: "", // 手机端的网址
    headerShow: true, // 新IM界面头部是否显示
    quickLabel: [],
  },
  mutations: {
    setRentCate(state, arr) {
      state.rentCate = arr;
    },
    setRightsCate(state, arr) {
      state.rightsCate = arr;
    },
    setRentList(state, arr) {
      state.rentList = arr;
    },
    setRightsList(state, arr) {
      arr = arr.map((item, i) => {
        return { ...item, title: item.m_title }
      })
      console.log(arr)
      state.rightsList = arr;
    },
    setScenicList(state, arr) {
      state.scenicList = arr;
    },
    setMaterialCate(state, arr) {
      state.materialCate = arr;
    },
    setExtractList(state, arr) {
      state.extractList = arr;
    },
    setmanageInfo(state, obj) {
      state.manageInfo = obj;
    },
    setPicTypeList(state, arr) {
      state.picTypeList = arr;
    },
    addbreadcrumbs(state, obj) {
      obj.beforeurl = window.location.pathname + window.location.search;
      state.breadcrumbs.push(obj);
    },
    setbreadcrumbs(state, arr) {
      state.breadcrumbs = arr;
    },
    setCoinCurrency(state, type) {
      state.coinCurrency = type;
    },
    setcarInfoCate(state, cate) {
      let arr = [];
      cate.data.map((item) => {
        let obj = {};
        obj.title = item.cate_id_cn;
        obj.id = item.cate_id;

        arr.push(obj);
      });
      for (var i = 0; i < arr.length; i++) {
        for (var j = i + 1; j < arr.length; j++) {
          if (arr[i].id == arr[j].id) {
            arr.splice(j, 1);
            j--;
          }
        }
      }
      state.carInfoCate = arr;
    },
    setFootPrintType(state, type) {
      state.footPrintType = type;
    },
    setTaskCate(state, cate) {
      state.taskCate = cate;
    },
    setcityinfoCate(state, cate) {
      state.cityinfoCate = cate;
    },
    setMoviesCate(state, moviesCate) {
      state.moviesCate = moviesCate;
    },
    setRewardPlugin(state, rewardPlugin) {
      state.rewardPlugin = rewardPlugin;
    },
    setBzList(state, bzList) {
      state.bzList = bzList;
    },
    setUserComplaintType(state, userComplaintType) {
      state.userComplaintType = userComplaintType;
    },
    setCourseCate(state, courseCate) {
      state.courseCate = courseCate;
    },
    setPayList(state, payList) {
      state.payList = payList;
    },
    setTypesOption(state, types_option) {
      state.types_option = types_option;
    },
    setLeftOpen(state, val) {
      state.leftopen = val;
    },
    setPluginForm(state, formList) {
      state.formList = formList;
    },
    set_chat_userinfo(state, chat_userinfo) {
      state.chat_userinfo = chat_userinfo;
    },
    setIsMailOpen(state, isMailOpen) {
      console.log(isMailOpen);
      state.isMailOpen = isMailOpen == "1" ? true : false;
    },
    setPhoneAddress(state, phoneAddress) {
      state.phoneAddress = phoneAddress;
    },
    startComputeCurrent(state) {
      state.intervalID = setInterval(() => {
        state.current = Date.now();
      }, 1000);
    },
    stopComputeCurrent(state) {
      clearInterval(state.intervalID);
      state.intervalID = 0;
    },
    setAdminTitle(state, adminTitle) {
      state.adminTitle = adminTitle;
    },
    setAddress(state, address) {
      state.address = address;
    },
    setPlugin(state, plugin) {
      state.plugin = plugin;
    },
    setIsLoading(state, isLoading) {
      state.isLoading = isLoading;
    },
    setCoinReatting(state, coinReatting) {
      state.coinReatting = coinReatting;
    },
    setQuickLabel(state, quickLabel) {
      state.quickLabel = quickLabel;
    },
    sendMenuListTree(state, obj) {
      state.menuList = obj.filter((item) => {
        if (item.z) {
          item.length = item.z.length;
          item.z = item.z.filter((it) => {
            if (it.z) {
              it.length = it.z.length;
              it.z = it.z.filter((t) => {
                if (t.z) {
                  t.length = t.z.length;
                  t.children = t.z;
                } else {
                  t.length = 0;
                }
                t.three = "三";
                t.index = 2;
                return t;
              });
              it.children = it.z;
            } else {
              it.length = 0;
            }

            it.three = "二";
            it.index = 1;
            return it;
          });
          item.children = item.z;
          item.three = "一";
          return item;
        } else {
          item.length = 0;
          item.three = "一";
          item.index = 0;
          return item;
        }
      });
      // state.menuList = obj;
    },
    sendNewsList(state, obj) {
      state.newsList = obj;
    },
    sendNewsCate(state, obj) {
      state.newsFeiLei = obj;
    },
    setId(state, id) {
      state.id = id;
    },
    setTitle(state, title) {
      state.title = title;
    },
    setTitle2(state, title2) {
      state.title2 = title2;
    },
    showTitle2(state, showTitle2) {
      state.showTitle2 = showTitle2;
    },
    setTzUrl(state, tzUrl) {
      state.tzUrl = tzUrl;
    },
    setParentTtile(state, parentTitle) {
      state.parentTitle = parentTitle;
    },
    setSmsId(state, sms_id) {
      state.sms_id = sms_id;
    },
    setWx_material(state, wx_material) {
      state.wx_material = wx_material;
    },
    setUpdateWxText(state, updateWxText) {
      state.updateWxText = updateWxText;
    },
    setUpdateNewRow(state, updateNewRow) {
      state.updateNewRow = updateNewRow;
    },
    setShopsItemId(state, shopItemId) {
      state.shopItemId = shopItemId;
    },
    setShopSlist(state, shopSlist) {
      state.shopSlist = shopSlist;
    },
    setShopCateList(state, shopCateList) {
      state.shopCateList = shopCateList;
    },
    setTourCateList(state, tourCateList) {
      state.tourCateList = tourCateList;
    },
    setRewardOption(state, rewardOption) {
      state.rewardOption = rewardOption;
    },
    setHeadTitleInfo(state, headTitleInfo) {
      state.headTitleInfo = headTitleInfo;
    },
    setAdminname(state, adminname) {
      state.adminname = adminname;
    },
    setisservice(state, isservice) {
      state.isservice = isservice;
    },
    setisnotice(state, val) {
      state.isNotice = val;
    },
    loginOut(state) {
      var params = {};
      api.login.logout(params).then((res) => {
        if (res.data.code == 200) {
          state.menuList = [];
          if (state.conversation && state.conversation.currentConversation.conversationID) {
            state.conversation.currentConversation.conversationID = "";
          }

          localStorage.removeItem("uid");
          // for (var i in state) {
          //   state[i] = "";
          // }
          localStorage.removeItem("admin_key");
          // localStorage.removeItem("store");
          localStorage.removeItem("adminuid");
          Cookies.remove("adminuid");
          Cookies.remove("admin_key");

          ElementUI.Message.success("退出成功");
          localStorage.removeItem("first_menu");
          router.replace("/login");
        }
      });
    },
    setsku(state, sku) {
      if (sku && sku.length > 0) {
        state.sku = sku.filter((item) => {
          if (item.z) {
            item.length = item.z.length;
            item.z = item.z.filter((it) => {
              if (it.z) {
                it.length = it.z.length;
                it.z = it.z.filter((t) => {
                  if (t.z) {
                    t.length = t.z.length;
                    t.children = t.z;
                  } else {
                    t.length = 0;
                  }
                  t.three = "三";
                  t.index = 2;
                  return t;
                });
                it.children = it.z;
              } else {
                it.length = 0;
              }

              it.three = "二";
              it.index = 1;
              return it;
            });
            item.children = item.z;
            item.three = "一";
            return item;
          } else {
            item.length = 0;
            item.three = "一";
            item.index = 0;
            return item;
          }
        });
      } else {
        state.sku = [];
      }
    },
    setRatting(state, ratting) {
      state.ratting = ratting;
    },
    setNovelClassify(state, type) {
      state.novelClassify = type;
    },
    setAudioClassify(state, type) {
      state.audioClassify = type;
    },
    setMobileWebSite(state, webSite) {
      state.mobileWebSite = webSite;
    },
    setHeaderShow(state, bollen) {
      state.headerShow = bollen;
    },
  },
  actions: {
    getRentCate(context) {
      api.getDz.get_rent_cate({ page_size: 99999 }).then((res) => {
        if (res.data.code == 200) {
          context.commit("setRentCate", res.data.result);
        }
      });
    },
    getRightsCate(context) {
      api.getDz.get_rights_cate({ page_size: 99999 }).then((res) => {
        if (res.data.code == 200) {
          context.commit("setRightsCate", res.data.result);
        }
      });
    },
    getRentList(context) {
      api.getDz.get_rent_list({ page_size: 99999 }).then((res) => {
        if (res.data.code == 200) {
          context.commit("setRentList", res.data.result);
        }
      });
    },
    getRightsList(context) {
      api.getDz.get_rights_list({ page_size: 99999 }).then((res) => {
        if (res.data.code == 200) {
          context.commit("setRightsList", res.data.result.data);
        }
      });
    },
    getScenicList(context) {
      api.getDz.get_scenic_setting({}).then((res) => {
        if (res.data.code == 200) {
          context.commit("setScenicList", res.data.result);
        }
      });
    },
    getCoinCurrency(context, obj) {
      var params = {};
      if (obj) {
        params = obj;
      }
      api.getCoin.get_coin_currency_coin(params).then((res) => {
        if (res.data.code == 200) {
          context.commit("setCoinCurrency", res.data.result);
        }
      });
    },
    getcarInfoCate(context) {
      api.getCharacter.getCharacterList({}).then((res) => {
        if (res.data.code == 200) {
          context.commit("setcarInfoCate", res.data.result);
        }
      });
    },
    getfootprinttype(context) {
      api.getPage.get_footprint_type({}).then((res) => {
        if (res.data.code == 200) {
          context.commit("setFootPrintType", res.data.result);
        }
      });
    },
    getcityinfoCate(context) {
      api.getClassify.getClassInfo({}).then((res) => {
        if (res.data.code == 200) {
          context.commit("setcityinfoCate", res.data.result);
        }
      });
    },
    getRewardPlugin(context) {
      api.getPlugin.getRewardPlugin({}).then((res) => {
        if (res.data.code == 200) {
          console.log(res);
          context.commit("setRewardPlugin", res.data.result);
        }
      });
    },
    getMailOpen(context, userSid) {
      userSid = userSid || "0";
      api.getUser.getMailDetail({ sid: userSid }).then((res) => {
        if (res.data.code == 200) {
          context.commit("setIsMailOpen", res.data.result.is_kdl);
        }
      });
    },
    getPlugin(context) {
      api.getIndex.getIndex_limits({}).then((res) => {
        console.log(res);
        if (res.data.code == 200) {
          localStorage.setItem("limit", JSON.stringify(res.data.result)); //存入 参数： 1.调用的值 2.所要存入的数据
          context.commit("setPlugin", res.data.result);
        }
      });
    },
    getHeadInfo(context) {
      var cate = "admin";
      var data = {
        cate,
      };
      api.getSettings.getSettingsList(data).then((res) => {
        if (res.data.code == 200 && res.data.status == 1) {
          for (var i in res.data.result) {
            if (res.data.result[i].iden == "ht_title") {
              document.title = res.data.result[i].value;
              context.commit("setAdminTitle", res.data.result[i].value);
            }
            if (res.data.result[i].iden == "ht_logo") {
              context.commit("setHeadTitleInfo", res.data.result[i].value);
            }
          }
        }
      });
    },
    getMenu(context) {
      var data = {
        parent_id: "0",
      };
      for (var i in data) {
        if (data[i] == undefined) {
          data[i] = "";
        }
      }
      if (data != undefined) {
        api.getMenu.getMenuList(data).then((res) => {
          if (res.data.code == 200) {
            context.commit("sendMenuListTree", res.data.result);
          }
        });
      }
    },
    getNewsCate(context) {
      var data = {
        parent_id: "0",
      };
      for (var i in data) {
        if (data[i] == undefined) {
          data[i] = "";
        }
      }
      api.getNewsFenlei.getNewsList(data).then((res) => {
        if (res.data.code == 200) {
          context.commit("sendNewsCate", res.data.result);
        }
      });
    },
    getWx_material(context, data) {
      var params = data;
      for (var i in params) {
        if (params[i] == undefined) {
          params[i] = "";
        }
      }
      api.getWechat.getMaterialList(params).then((res) => {
        if (res.data.code == 200 && res.data.status == 1) {
          context.commit("setWx_material", res.data.result);
        }
      });
    },
    getShopsAll(context, data) {
      console.log(data);
      var params = data;
      for (var i in params) {
        if (params[i] == undefined) {
          params[i] = "";
        }
      }
      var url = router.history.current.fullPath;
      if (url.indexOf("index/tour") >= 0) {
        params.types = 9;
      }
      api.getShops.getShopsList(params).then((res) => {
        if (res.data.code == 200 && res.data.status == 1) {
          context.commit("setShopSlist", res.data.result);
        }
      });
    },
    getShopsCate(context) {
      var params = {};
      params.parent_id = "";
      for (var i in params) {
        if (params[i] == undefined) {
          params[i] = "";
        }
      }
      api.getShops.getProductCateTreeList(params).then((res) => {
        if (res.data.code == 200 && res.data.status == 1) {
          context.commit("setShopCateList", res.data.result);
        }
      });
    },
    getTourCate(context) {
      var params = {};
      params.parent_id = "";
      for (var i in params) {
        if (params[i] == undefined) {
          params[i] = "";
        }
      }
      api.getTour.getProductCateTreeList(params).then((res) => {
        if (res.data.code == 200 && res.data.status == 1) {
          context.commit("setTourCateList", res.data.result);
        }
      });
    },
    getNewsList(context, data) {
      var params = data;
      for (var i in data) {
        if (data[i] == undefined) {
          data[i] = "";
        }
      }
      api.getNews.getNewsList(params).then((res) => {
        if (res.data.code == 200 && res.data.status == 1) {
          context.commit("sendNewsList", res.data.result);
        }
      });
    },
    getSkuListTree(context) {
      var params = {};
      params.parent_id = "0";
      api.getShops.getSkuListTree(params).then((res) => {
        if (res.data.code == 200 && res.data.status == 1) {
          var arr = [];
          if (res.data.result) {
            context.commit("setsku", res.data.result);
          } else {
            context.commit("setsku", arr);
          }
        }
      });
    },
    getRatting(context) {
      api.getUser
        .getRatingList({
          parent_id: "0",
        })
        .then((res) => {
          console.log(res);
          if (res.data.code == 200) {
            context.commit("setRatting", res.data.result);
          }
        });
    },
    getNovelClassify(context, obj) {
      // var params = {};
      // if (obj) {
      //   params = obj;
      // }
      api.getNovel.getNovelClassify({}).then((res) => {
        if (res.data.code == 200) {
          context.commit("setNovelClassify", res.data.result);
        }
      });
    },
    getAudioClassify(context, obj) {
      // console.log("3333333333333333:",obj)
      // var params = {};
      // if (obj) {
      //   params = obj;
      // }
      api.getNovel.getAudioClassify({}).then((res) => {
        if (res.data.code == 200) {
          context.commit("setAudioClassify", res.data.result);
        }
      });
    },
    getMobileWebSite(context) {
      api.getSettings.getSettingsList({ cate: "wx" }).then((res) => {
        if (res.data.code == 200 && res.data.status == 1) {
          res.data.result.forEach((item) => {
            if (item.title == "手机端网址") {
              context.commit("setMobileWebSite", item.value);
            }
          });
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
      });
    },
  },
  modules: {
    conversation,
    group,
    friend,
    blacklist,
    imUser,
    modulePage,
    live,
  },
});
